package ug.co.icom.kyazze.healthticket.services;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import ug.co.icom.kyazze.healthticket.DataBase.Database;
import ug.co.icom.kyazze.healthticket.data.MyConnections;
import android.app.IntentService;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

public class IntentServiceGetConnections extends IntentService {
	private static String TAG = "IntentServiceGetConnections";
	Database query_handler;
	static BasicDBList connrequests = null;
	static String OriginalObject = "";
	static String server_output = null;
	static String temp_output = null;
	static String output = null;
	static String outputy = null;
	
	public IntentServiceGetConnections() {
		super(TAG);
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Log.d(TAG, "OnHandleIntent");

		ApplicationObject mApplication = (ApplicationObject) getApplicationContext();
		@SuppressWarnings("unused")
		Long userName = mApplication.getphone_number(); // get user id (phone
														// number)

		try {

//			QueryBuilder qb = new QueryBuilder();
//			URL url = new URL(qb.buildLoginUrl(userName, password));
//			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			
			URL url = new URL(
					"https://api.mongohq.com/databases/mHealth/collections/personal_details/documents/53078a0f98475f0002000323/?_apikey=8qvfwj4yp5q8ikntgvta");
			HttpURLConnection conn = (HttpURLConnection) url
					.openConnection();
			
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Accept", "application/json");

			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			System.out.println("Output from Server .... \n");
			while ((outputy = br.readLine()) != null) {
				output = outputy;
			}
//			
//			// Remove square brackets
//			OriginalObject = output.substring(1, output.length() - 1);

			Object o = com.mongodb.util.JSON.parse(output);

			DBObject dbObj = (DBObject) o;
			connrequests = (BasicDBList) dbObj.get("connrequests");
			

			//AllergyData allergy_data = new AllergyData(MainSec.this); DB insert

			MyConnections temp = new MyConnections();
			

			SQLiteDatabase db = openOrCreateDatabase("mrDB", MODE_PRIVATE, null);
			Database database = new Database(db);

//			ArrayList<Allergy> all_object = query_handler
//					.getUnSyc_AllAllergies();


			for (Object obj : connrequests) {

				BasicDBObject healthObj = (BasicDBObject) obj;

				temp.profileimage = healthObj.get("profileimage").toString();
				temp.uid = healthObj.get("uid").toString();
				temp.firstname = healthObj.get("firstname").toString();
				temp.lastname = healthObj.get("lastname").toString();
				temp.othernames = healthObj.get("othernames").toString();
				temp.phonenumber = healthObj.get("phonenumber").toString();
				temp.dateofbirth = healthObj.get("dateofbirth").toString();			
				temp.allergies = healthObj.get("allergies").toString();
				temp.medication = healthObj.get("medication").toString();
				temp.immunization = healthObj.get("immunization").toString();
				temp.labdata = healthObj.get("labdata").toString();
				temp.conditions = healthObj.get("conditions").toString();
				temp.imaging = healthObj.get("imaging").toString();
				temp.insurance = healthObj.get("insurance").toString();
				temp.surgeries = healthObj.get("surgeries").toString();
				
				//if(healthObj.get("accepted").toString() =="true"){
					database.insertConnection(temp);
				//}
			
				//allergy_data.insert_allergy(allergy_obj);
				
				

			}
			db.close();

		} catch (Exception e) {
			e.getMessage();
		}
	}
	
	//
	


}
